/* *
 * @program: VideoDirector
 *
 * @description: ${description}
 *
 * @author: 909845
 *
 * @create: 2018-12-25 15:41
***/
#include "TsRemux.h"
#include "../Public/PublicTools.h"

#define TAG "TID[" << m_strTaskId << "]- "

CTsRemux::CTsRemux(const string &strTaskID) : m_strTaskId(strTaskID)
{
    DEBUG_LOG(TAG << "Create CTsRemux");
}

CTsRemux::~CTsRemux()
{
    DEBUG_LOG(TAG << "Destroy ~CTsRemux");
}

Bool CTsRemux::Initlize(const FuncRemuxStreamCb remuxStreamCb, bool bHaveAudio)
{
    Bool bRet = False;
    do
    {
        Assert(remuxStreamCb != nullptr);
        m_remuxStreamCb = remuxStreamCb;
        m_muxItem.bHasAudioPid = bHaveAudio;
        bRet = True;
    } while (0);

    return bRet;
}

int CTsRemux::OnTsPacket(unsigned char *lpszBuf, int nLen, long long lTimestamp)
{
    m_remuxStreamCb((const char*)lpszBuf, nLen);
    return nLen;
}

MuxItem *CTsRemux::GetMuxItem()
{
    return &m_muxItem;
}


void CTsRemux::RecvVideoStream(const char *pData, int nLen, unsigned int uTimestamp)
{
    if (!m_bStartSps)
    {
        const EH264NalType type = CH264Tools::GetNalType((const UInt8 *) pData, nLen, CH264Tools::GetH264StartNum((const UInt8 *) pData, nLen));
        if (EH264NAL_SPS != type)
        {
            TRACE_LOG(TAG << "Wait for SPS.");
            return;
        }
        m_bStartSps = true;
    }

    if (m_bStartSps)
    {
        WriteBuf2TsFile(CConstVal::N_VIDEO_FRAME, ESTREAM_VIDEO, (unsigned char *) pData, nLen, uTimestamp * 90, 0, this);
    }
}

void CTsRemux::RecvAudioStream(const char *pData, int nLen, unsigned int uTimestamp)
{
    if (m_bStartSps)
    {
        WriteBuf2TsFile(CConstVal::N_VIDEO_FRAME, ESTREAM_VIDEO, (unsigned char *) pData, nLen, uTimestamp * 90, 0, this);
    }
}


